<?php
/**
 * 对新入职员工以及转正员工的处理时的一些通用方法
 */

require_once 'phpmailer/class.phpmailer.php';
apf_require_class('Bll_Attendance_EmailLog');

class Tools_NewGuys {
    private static $instance;

    public static function get_instance(){
        if(!self::$instance) {
            self::$instance = new Tools_NewGuys();
        }   
        return self::$instance;
    }
    
    public static function check_mail($mail) {
        if (empty($mail)) {
            self::log("即使是测试，你也得给我个邮箱嘛！");
            return FALSE;
        } elseif (!preg_match("#^([a-zA-Z0-9_\.\-])+@\w+([-.]\w+)*\.\w+([-.]\w+)*$#",$mail)) {
            self::log("好说歹说，你总得给我个正确的邮箱啊！");
            return FALSE;
        } else {
            return TRUE;
        } 
    }

    static function log($str = '') {
        echo PHP_EOL . date('Y-m-d H:i:s') . ": " . $str . PHP_EOL;
    }

    public function send ($to, $cc, $title, $body, $from = "hr@anjuke.com", $file = null) {
        if (empty($to)) {
            return "no email exsit";
        }

        $mail = new PHPMailer(true);
    
        $mail->CharSet = "UTF-8";
        $mail->IsSMTP();
        $mail->SMTPDebug = 0;

        $mail->SMTPAuth = true;
        $mail->Host = "smtp.anjuke.com";
        $mail->Port = 25;
        $mail->Username = "hr@anjuke.com";
        $mail->Password = "anjuke1qazxsw2";
        $mail->AddReplyTo('shhr@anjuke.com', '人力资源部');
        $mail->AddAddress($to, $to);
        $mail->SetFrom($from, '人力资源部');
        $mail->Subject = $title;
        $mail->AltBody = ' ';
        $mail->MsgHTML($body);
        $mail->IsHTML(true);

        if ($cc) {
            $mail->AddCC($cc, $cc);    
        }

        if ($file) {
            $mail->AddAttachment($file); // attachment
            //$mail->AddEmbeddedImage($file,'imgn','hellohr.jpg','quoted-printable','image/jpeg');
            //$mail->Body = '<html><body><div>gogog<img src="cid:imgn" /></div></body></html>';
        }

        try { 
            $result = $mail->send();
            return $result;
        } catch (Exception $e) {
            return FALSE;
        }

        return $result;


    }
}
